package com.ccys.cms.service.discount.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ccys.cms.service.discount.PromoCodeService;
import com.ccys.common.util.Kit;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.discount.PromoCodeDto;
import com.ccys.db.dto.discount.PromoCodeMemberDto;
import com.ccys.db.entity.TblPlan;
import com.ccys.db.entity.TblPromoCode;
import com.ccys.db.entity.TblPromoCodeMember;
import com.ccys.db.mapper.TblPlanMapper;
import com.ccys.db.mapper.TblPromoCodeMapper;
import com.ccys.db.mapper.TblPromoCodeMemberMapper;
import com.ccys.db.page.PageDto;
import com.ccys.db.rely.service.impl.BaseServiceImpl;
import com.ccys.db.service.BaseTblAuditTrailWebService;
import com.ccys.db.vo.discount.PromoCodeFindListSelectVo;
import com.ccys.db.vo.discount.PromoCodeMemberFindByCodeIdVo;
import com.ccys.db.vo.discount.PromoCodePageQueryVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: WhiteCoat
 * @description: 优惠卷接口实现类
 * @author: ZhangJingCan
 * @create: 2023-12-15 13:47
 **/

@Service
public class PromoCodeServiceImpl extends BaseServiceImpl<TblPromoCode> implements PromoCodeService {

    @Resource
    private TblPromoCodeMapper tblPromoCodeMapper;

    /**
     * 配套
     */
    @Resource
    private TblPlanMapper tblPlanMapper;

    /**
     * 优惠卷限制表 - 忽略表
     */
    @Resource
    private TblPromoCodeMemberMapper tblPromoCodeMemberMapper;

    /**
     * 用于保存修改状态日志
     */
    @Autowired(required = false)
    private BaseTblAuditTrailWebService baseTblAuditTrailWebService;

    @Override
    public ResponseResult<List<TblPlan>, Object> findPlanByType(Integer type) {
        return ResponseResult.success(tblPlanMapper.findPlanByType(type));
    }

    @Override
    public ResponseResult findPromoCodeByCode(String code) {
        //根据传入的code查询是否存在 - 存在则返回错误信息，不存在则返回success
        TblPromoCode tblPromoCode = tblPromoCodeMapper.findPromoCodeByCode(code);
        if (ObjectUtil.isNotEmpty(tblPromoCode)) {
            return ResponseResult.error("The current code already exists!");
        } else {
            return ResponseResult.success();
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult savePromoCode(PromoCodeDto dto) {
        return ResponseResult.update(this.insertOrUpdate(dto));
    }

    @Override
    public ResponseResult updatePromoCodeStatus(PromoCodeDto dto) {
        //校验
        if (ObjectUtil.isEmpty(dto.getId()) || ObjectUtil.isEmpty(dto.getStatus())) {
            return ResponseResult.error("Parameter Error!");
        }
        //准备数据修改
        TblPromoCode tblPromoCode = new TblPromoCode();
        tblPromoCode.setId(dto.getId());
        tblPromoCode.setStatus(dto.getStatus());
        if (tblPromoCodeMapper.updateByPrimaryKeySelective(tblPromoCode) > 0) {
            baseTblAuditTrailWebService.saveStatusLog(dto, dto.getStatus());
            return ResponseResult.success("Modified successfully!");
        }
        return ResponseResult.error("Modification failed!");
    }

    @Override
    public ResponseResult<PageInfo<PromoCodePageQueryVo>, Object> findPagePromoCode(PromoCodeDto dto) {
        PageDto.startPage();
        List<PromoCodePageQueryVo> list = tblPromoCodeMapper.findPagePromoCode(dto);
        PageInfo<PromoCodePageQueryVo> info = new PageInfo<>(list);
        return ResponseResult.success(info);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult deletePromoCode(PromoCodeDto dto) {
        //判断
        if (ObjectUtil.isEmpty(dto.getId())) {
            return ResponseResult.error("Parameter Error!");
        }
        //准备实体类并赋值
        TblPromoCode tblPromoCode = new TblPromoCode();
        tblPromoCode.setId(dto.getId());
        tblPromoCode.setDelFlag(1);
        //判断是否修改成功 - 返回不同状态
        if (tblPromoCodeMapper.updateByPrimaryKeySelective(tblPromoCode) > 0) {
            return ResponseResult.success("Delete Successful!");
        }
        return ResponseResult.error("Delete Failed!");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult addPromoCodeMember(PromoCodeMemberDto dto) {
        //校验字段值
        Kit.allParamValid(dto, "promoCodeId", "memberId", "allowChild");
        //检查是否已添加当前用户
        Example promoCodeMemberExample = new Example(TblPromoCodeMember.class);
        promoCodeMemberExample.createCriteria()
                .andEqualTo("promoCodeId", dto.getPromoCodeId())
                .andEqualTo("memberId", dto.getMemberId())
                .andEqualTo("delFlag", 0);
        //存在即返回错误信息
        if (ObjectUtil.isNotEmpty(tblPromoCodeMemberMapper.selectOneByExample(promoCodeMemberExample))) {
            return ResponseResult.error("The current user is already in the list, please do not add again!");
        }
        //开始保存
        if (tblPromoCodeMemberMapper.insertSelective(dto) > 0) {
            return ResponseResult.success("Added successfully!");
        }
        return ResponseResult.error("Add failed!");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult deletePromoCodeMember(PromoCodeMemberDto dto) {
        //判断
        if (ObjectUtil.isEmpty(dto.getId())) {
            return ResponseResult.error("Parameter Error!");
        }
        //准备实体类并赋值
        TblPromoCodeMember tblPromoCodeMember = new TblPromoCodeMember();
        tblPromoCodeMember.setId(dto.getId());
        tblPromoCodeMember.setDelFlag(1);
        //判断是否修改成功 - 返回不同状态
        if (tblPromoCodeMemberMapper.updateByPrimaryKeySelective(tblPromoCodeMember) > 0) {
            return ResponseResult.success("Delete Successful!");
        }
        return ResponseResult.error("Delete Failed!");
    }

    @Override
    public ResponseResult<PageInfo<PromoCodeMemberFindByCodeIdVo>, Object> findPromoCodeMember(PromoCodeDto dto) {
        //判断
        if (ObjectUtil.isEmpty(dto.getId())) {
            return ResponseResult.error("Parameter Error!");
        }
        PageDto.startPage();
        PageInfo<PromoCodeMemberFindByCodeIdVo> info = new PageInfo<>(tblPromoCodeMemberMapper.findPromoCodeMember(dto.getId()));
        return ResponseResult.success(info);
    }

    @Override
    public ResponseResult<List<PromoCodeFindListSelectVo>, Object> findListSelect() {
        List<PromoCodeFindListSelectVo> vos = tblPromoCodeMapper.findListSelect();
        return ResponseResult.success(vos);
    }
}
